/**
 * @author: Jam
 * @create: 2018-03-26 17:05
 */
package com.byxy.service.impl;

import com.byxy.dao.AdminRuleDao;
import com.byxy.dao.AdminUserDao;
import com.byxy.model.AdminUser;
import com.byxy.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserDao adminUserDao;
    @Autowired
    private AdminRuleDao adminRuleDao;

    @Override
    public void save(AdminUser adminUser) {
        adminUserDao.save(adminUser);
    }

    @Override
    public void delete(Integer[] ids) {
        for (int id : ids) {
            adminUserDao.delete(id);
        }
    }

    @Override
    public void update(AdminUser adminUser) {
        adminUserDao.update(adminUser);
    }

    @Override
    public AdminUser get(Integer id) {
        return adminUserDao.get(id);
    }

    @Override
    public List<AdminUser> listPage(Integer pageNum, Integer pageSize, String search) {
        return adminUserDao.listPage(pageNum, pageSize, search);
    }

    @Override
    public Integer count(String search) {
        return adminUserDao.count(search);
    }

    @Override
    public void enable(Integer[] ids) {
        for (int id : ids) {
            AdminUser adminUser = adminUserDao.get(id);
            adminUser.setStatus(true);
            adminUserDao.update(adminUser);
        }
    }

    @Override
    public void disable(Integer[] ids) {
        for (int id : ids) {
            AdminUser adminUser = adminUserDao.get(id);
            adminUser.setStatus(false);
            adminUserDao.update(adminUser);
        }
    }

    @Override
    public AdminUser getLogin(String username, String password) {
        return adminUserDao.login(username, password);
    }

    @Override
    public Boolean hasRule(AdminUser adminUser) {
        // 使用中间表去实现, 否则请处理冗余数据的问题
//        String rule = adminUser.getAdminGroup().getRules();
//        int[] rules = TypeConverter.toIntArray(rule);
//        for (int i : rules) {
//            System.out.println(rules[i]);
//        }
//        System.out.println("-------------");
//        for (int i : rules) {
//            System.out.println(i);
//            System.out.println(rules[i]);
//            AdminRule adminRule = adminRuleDao.get(rules[i]);
//            System.out.println(adminRule);
////                if (null != adminRule.getPath()) {
////                    System.out.println(adminRule.getPath());
////                }
//        }
//        System.out.println(adminUser);
//        System.out.println(adminUser.getAdminGroup().getAdminRules());
//        AdminUser adminUser1 = adminUserDao.get(adminUser.getId());
//        System.out.println(adminUser1.getAdminGroup().getAdminRules());
//        Set<AdminRule> set = adminUser.getAdminGroup().getAdminRules();
//        for (AdminRule e : set) {
//            System.out.println(e.getPath());
//        }
        return true;
    }
}
